import asyncio
import aiohttp
import json

async def test_text_qa_direct():
    """直接测试文本QA服务的predict接口"""
    print("开始直接测试文本QA服务...")
    
    url = "http://localhost:8001/predict"
    headers = {
        "Content-Type": "application/json"
    }
    
    # 测试数据
    test_data = {
        "query": "测试问题",
        "session_id": "test_session_123",
        "task_id": "test_task_123"
    }
    
    try:
        async with aiohttp.ClientSession() as session:
            async with session.post(url, headers=headers, json=test_data) as response:
                print(f"响应状态码: {response.status}")
                response_text = await response.text()
                print(f"响应内容: {response_text}")
                
                # 尝试解析JSON
                try:
                    response_json = json.loads(response_text)
                    print("响应JSON解析成功")
                    print(f"code: {response_json.get('code')}")
                    print(f"message: {response_json.get('message')}")
                    print(f"data: {response_json.get('data')}")
                except json.JSONDecodeError as e:
                    print(f"响应不是有效的JSON: {e}")
    
    except Exception as e:
        print(f"测试过程中发生错误: {str(e)}")

async def test_text_qa_api_run():
    """测试文本QA服务的/api/run接口（编排器调用的接口）"""
    print("\n开始测试文本QA服务的/api/run接口（编排器调用）...")
    
    url = "http://localhost:8001/api/run"
    headers = {
        "Content-Type": "application/json"
    }
    
    # 模拟编排器发送的请求数据
    test_data = {
        "task_id": "test_orchestrator_task",
        "inputs": {
            "query": "测试问题",
            "session_id": "test_session_123"
        },
        "agent_params": {}
    }
    
    try:
        async with aiohttp.ClientSession() as session:
            async with session.post(url, headers=headers, json=test_data) as response:
                print(f"响应状态码: {response.status}")
                response_text = await response.text()
                print(f"响应内容: {response_text}")
                
                # 尝试解析JSON
                try:
                    response_json = json.loads(response_text)
                    print("响应JSON解析成功")
                    print(f"code: {response_json.get('code')}")
                    print(f"message: {response_json.get('message')}")
                    print(f"data: {response_json.get('data')}")
                except json.JSONDecodeError as e:
                    print(f"响应不是有效的JSON: {e}")
    
    except Exception as e:
        print(f"测试过程中发生错误: {str(e)}")

async def test_text_qa_health():
    """测试文本QA服务的健康检查接口"""
    print("\n开始测试文本QA服务的健康检查接口...")
    
    url = "http://localhost:8001/"
    
    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                print(f"响应状态码: {response.status}")
                response_text = await response.text()
                print(f"响应内容: {response_text}")
    
    except Exception as e:
        print(f"测试过程中发生错误: {str(e)}")

async def test_text_qa_process():
    """测试文本QA服务的process接口（兼容旧版本）"""
    print("\n开始测试文本QA服务的process接口（兼容旧版本）...")
    
    url = "http://localhost:8001/process"
    headers = {
        "Content-Type": "application/json"
    }
    
    test_data = {
        "query": "测试问题",
        "session_id": "test_session_123"
    }
    
    try:
        async with aiohttp.ClientSession() as session:
            async with session.post(url, headers=headers, json=test_data) as response:
                print(f"响应状态码: {response.status}")
                response_text = await response.text()
                print(f"响应内容: {response_text}")
                
                # 尝试解析JSON
                try:
                    response_json = json.loads(response_text)
                    print("响应JSON解析成功")
                    print(f"结果: {response_json}")
                except json.JSONDecodeError as e:
                    print(f"响应不是有效的JSON: {e}")
    
    except Exception as e:
        print(f"测试过程中发生错误: {str(e)}")

async def main():
    # 运行所有测试
    await test_text_qa_health()
    await test_text_qa_direct()
    await test_text_qa_api_run()
    await test_text_qa_process()
    
    print("\n文本QA服务测试完成！")

if __name__ == "__main__":
    asyncio.run(main())